/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "gimi.h"

int main(int, char **) {
  /******************/
  debugInit();
  debugSetGlobalDebugLvl(99);
  debugSetLogFilename("sockettest.log");
  /******************/
  
  gimi::HubSocketHandler sock(2000);
  
  sock.initializeSocket(SOCKET_TYPE_TCP);
  
  int result = sock.openSocket("localhost", 1666);
  
  dPrint(1, "Got result (%d) %s", result, gimi::getGimiErrorString(result).c_str());
  
  if (result == GIMI_OK) {
    char *data;
    int datalength = 0;
    
    dPrint(1, "Send stuff to socket");
    std::string dataString = "abcdefghiklmn\n";
    int stringLength = dataString.size();
    
    sock.sendHubMessageToSocket(dataString.c_str(), stringLength);
    dPrint(1, "Stuff sent.");
    
    int receiveResult = GIMI_OK;
    while (receiveResult == GIMI_OK || receiveResult == GIMI_INVALIDHUBDATA) {
      dPrint(1, "Receiveloop.");
      receiveResult = sock.readHubMessageFromSocket(&data, &datalength);
      dPrint(1, "Got result (%d) %s", receiveResult, gimi::getGimiErrorString(receiveResult).c_str());
    }
  }
  
  return 0;
}
